package com.ld.poetry.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.ld.poetry.config.LoginCheck;
import com.ld.poetry.config.PoetryResult;
import com.ld.poetry.entity.PhotoWall;
import com.ld.poetry.service.PhotoWallService;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 照片墙(PhotoWall)表控制层
 *
 * @author makejava
 * @since 2023-08-19 22:45:06
 */
@RestController
@RequestMapping("photoWall")
public class PhotoWallController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private PhotoWallService photoWallService;

    /**
     * 分页查询所有数据
     *
     * @param page      分页对象
     * @param photoWall 查询实体
     * @return 所有数据
     */
    @GetMapping("/selectAll")
    public PoetryResult selectAll(Page<PhotoWall> page, PhotoWall photoWall) {
        return PoetryResult.success(this.photoWallService.page(page, new QueryWrapper<>(photoWall)));
    }
    /**
     * 查询所有数据
     *
     * @param photoWall 查询实体
     * @return 所有数据
     */
    @GetMapping("/list")
    public PoetryResult list(PhotoWall photoWall) {

        List<PhotoWall> list = this.photoWallService.list(new QueryWrapper<>(photoWall));

        return PoetryResult.success(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @LoginCheck
    public PoetryResult selectOne(@PathVariable Serializable id) {
        return PoetryResult.success(this.photoWallService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param photoWall 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    @LoginCheck
    public PoetryResult insert(@RequestBody PhotoWall photoWall) {
        return PoetryResult.success(this.photoWallService.save(photoWall));
    }
//    @RequestMapping("/add")
//    @LoginCheck
//    public PoetryResult insert(PhotoWall photoWall) {
//        return PoetryResult.success(this.photoWallService.save(photoWall));
//    }
    /**
     * 修改数据
     *
     * @param photoWall 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @LoginCheck
    public PoetryResult update(@RequestBody PhotoWall photoWall) {
        return PoetryResult.success(this.photoWallService.updateById(photoWall));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @LoginCheck
    public PoetryResult delete(@RequestParam("id") List<Long> idList) {
        return PoetryResult.success(this.photoWallService.removeByIds(idList));
    }


    /**
     * 自己写的增删改查
     */
    @PostMapping ("tolist")
    public PoetryResult tolist(@RequestBody PhotoWall photoWall){
        // 创建 Page 对象，指定每页大小和当前页
        Page<PhotoWall> page = new Page<PhotoWall>(photoWall.getPageNum(), photoWall.getPageSize());
        // 设置查询条件（可选）
        QueryWrapper<PhotoWall> queryWrapper = new QueryWrapper<PhotoWall>();
        queryWrapper.eq(ObjectUtil.isNotEmpty(photoWall.getId()),"id" ,photoWall.getId());
        queryWrapper.like(ObjectUtil.isNotEmpty(photoWall.getRemark()),"remark" ,photoWall.getRemark());
        // 调用分页查询方法
        IPage<PhotoWall> userPage = photoWallService.queryPhotoWall(page, queryWrapper);
        return PoetryResult.success(userPage);
    }

    @PostMapping ("toupdate")
    public PoetryResult toupdate( @RequestBody PhotoWall photoWallVo){
        boolean b = photoWallService.updateById(photoWallVo);
        if (b){
            return PoetryResult.success(null);
        }else {
            return PoetryResult.fail("修改失败");
        }
    }

    @GetMapping("todel")
    public PoetryResult todel(Integer id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        boolean remove = photoWallService.remove(queryWrapper);
        if (remove){
            return PoetryResult.success(null);
        }else {
            return PoetryResult.fail("删除失败");
        }
    }
    @PostMapping("toadd")
    public PoetryResult toadd(@RequestBody PhotoWall photoWallVo){
        boolean save = photoWallService.save(photoWallVo);
        if (save){
            return PoetryResult.success(null);
        }else {
            return PoetryResult.fail("添加失败");
        }
    }
    @GetMapping("delOrUpdate")
    public PoetryResult delOrUpdate( PhotoWall photoWallVo){
        boolean save = photoWallService.saveOrUpdate(photoWallVo);
        if (save){
            return PoetryResult.success(null);
        }else {
            return PoetryResult.fail("添加或修改失败");
        }
    }

//    /**
//     * 我自己的邮件工具
//     */
//    @Autowired
//    JavaMailSender javaMailSender;
//    @GetMapping ("toEmail")
//    public PoetryResult toEmail(String toEmail,String title,String body){
//        EmailUtils.sendEmail(javaMailSender,toEmail,title,body);
//        return  PoetryResult.success();
//    }
}

